
var ipc = require('ipc');

H5x.component("package.package",["common.list"],function(opts){   
    this.container =  opts.container;
    this.opts = opts;
    this.use("render");

    this.use("dai",{
        app_name:"home",
        host:global_api_host
    });

    //this.use("emitter");
    this.use("ipc");

    this.activitydai = this.dai.create("activity"); 
    this.applydai = this.dai.create("apply"); 
    this.choosendai = this.dai.create("choosen"); 

    //this.applistdai = this.dai.create("appsetting"); 
    this.init(); 

},{

    init:function(){
        this.opts.paystatus = 1;
        this.load_page();
        /*
        var self = this;
        this.activitydai.query({},function(rs){
            self.render("tp-export-export");

            $(".ac-selector").make("ac-selector",{
                aclist:rs.list,
                onchange:function(opts){
                    self.list_apply(opts);
                }
            });
            
        })
        */
        
       // $(".pager-nav").make("pager");
    },

    on_render_list: function(rs) {
        var activityno = $("#select-activity option:selected").attr("data-no");
        this.render("tp-package-lit", {
            activityno:activityno,
            applylist: rs.list
        }, function(html) {
            //console.log(html);
            $("#common-item-list").html(html);
        });

        this.bindApplyAction();
    },

    /*
    init:function(){

        this.opts.pager = {
            page: 0,
            size: 10
        }

        var self = this;
        this.activitydai.query({},function(rs){
            self.render("tp-package-package");

            $(".ac-selector").make("ac-selector",{
                aclist:rs.list,
                onchange:function(q){
                    self.opts.list_query = q;
                    self.list_apply();
                }
            });
            
        })
        
        $(".pager-nav").make("pager");
    },

    list_apply:function(opts){
        var self = this;

        var q = this.opts.list_query
        var query = {
            _orderd: "no",
            _page: this.opts.pager.page,
            _size: this.opts.pager.size,
            activityid:q.activityid,
            status:3
        };

        if(q.clazz != "all"){
            query.clazz = q.clazz;
        }

        self.applydai.query(query,function(rs1){
            //var activityno = $("#select-activity option:selected").attr("data-no");
            self.render("tp-package-lit",{
                activityno:q.activityno,
                applylist:rs1.list
            },function(html){
                $("#applylist-container").html(html); 
                self.bindApplyAction();

                //分页
                $(".pager-nav").make("pager", {
                    pager: rs1.pager,
                    onchange: function(newpage, curpage) {
                        self.opts.pager.page = newpage;
                        self.list_apply();
                        //console.log(newpage,curpage);
                    }
                });               
            });            
        });
    },

    */

    bindApplyAction : function(){
        var self = this;
        
        //打开目录
        $(".open-finish").click(function(){

            $("body").mLoading({
                text:"正在打开目录...",
                mask:false
            });

            var dir = $(this).parent().attr("data-dir");
            self.ipc("opendir",{
                path:dir,
                finish:1,
            },function(rs){
                console.log(rs);
                setTimeout(function(){
                    $("body").mLoading("hide");
                },500);
                
            });
        });


        //压缩上传
        $(".package,.repackage").click(function(){
            var dir = $(this).parent().attr("data-dir");
            var applyid = $(this).parent().attr("data-id"); 

            self.uploadPackage(dir,applyid);
        });

        //发送短信
        $(".send-sms-email").click(function(){
            var applyid = $(this).parent().attr("data-id"); 
            var dir = $(this).parent().attr("data-dir");
            self.applydai.action("choosecode-sms",{
                applyid:applyid,
                type:2,
                dir:dir
            },function(rs){
                $.tip(rs.message);
                if(rs.code>0){
                    self.load_list();
                }
            });
        });
    },

    uploadPackage : function(dir,applyid){
        $("#up-progress-dialog").modal();

        var com_progress = $("#up-progress-dialog .modal-dialog").make("upprogress",{
            title:"打包并上传照片",
            tip:"正在打包照片..."
        });

        var self = this;
        
        self.ipc("package",{
            path:dir,
            applyid:applyid
        },function(rs){
            if(rs.code<0){
                $.tip(rs.message);
            }else if(rs.progress){
                com_progress.emit(rs);
            }else if(rs.end){                
                self.applydai.update({id:applyid},{
                    upstatus:3
                },function(rs1){
                    self.load_list();
                });
            }
        });
    }
});